package com.hjwoo.app.utils;

import android.text.format.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Create by ：rxiaowen on 2016/8/3 10:19
 * Email：rxiaowen@sina.com
 */
public class MyTimeUtils {

    /*
    * 获取系统当前时间
    * */
    public static String getSystemDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    /*
     *获取明天的日期
     */
    public static String getTomDate() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    /*
    * 获取系统当前24小时时间
    * **/
    public static int getSystemHour24() {
        //通过DateFormat获取系统的时间
        String currentTime = DateFormat.format("HH", new Date()).toString();
        //通过SimpleDateFormat获取24小时制时间
        SimpleDateFormat sdf = new SimpleDateFormat("HH", Locale.getDefault());
        currentTime = sdf.format(new Date());
        int currTime = Integer.parseInt(currentTime);
        return currTime;
    }

    /*
    *将String转为Date
    * */
    public static Date str2Date(String str) {
        String pattern = "MM月dd日"; //首先定义时间格式
        SimpleDateFormat format = new SimpleDateFormat(pattern);//然后创建一个日期格式化类
        Date convertResult = null;
        try {
            convertResult = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return convertResult;
    }

    /*
     *获取当前几月几日
     * */
    public static String getMonthAndDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    /*
    * 将string字符串8月4日
    * 转成08-04
    * */
    public static String str2str(String str) {
        String pattern = "MM-dd"; //首先定义时间格式
        SimpleDateFormat format = new SimpleDateFormat(pattern);//然后创建一个日期格式化类
        Date convertResult = null;
        try {
            convertResult = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String resultTime = Date2String(convertResult);
        return resultTime;
    }

    /*
    * Date转为String
    * */
    public static String Date2String(Date date) {
        String dateStr = new SimpleDateFormat("MM月dd日").format(date);
        return dateStr;

    }

    /*
     * 获取系统当前几时
     * */
    public static int getSystemHour12() {
        Calendar ca = Calendar.getInstance();
        int hour = ca.get(Calendar.HOUR);
        return hour;
    }

    /*
    * 获取系统当前年份
    * */
    public static String getSystemYear() {
        Calendar ca = Calendar.getInstance();
        int year = ca.get(Calendar.YEAR);
        String yearStr = String.valueOf(year);
        return yearStr;
    }

    //当前时间加几天,生成几天后的时间
    public static Date addDays(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, days);
        return c.getTime();
    }
}